#include "talisman.h"
#include "Map\Space.h"
#include "Map\Map.h"
#include "Map\PositionKeeper.h"
#include "Character\PlayerCharacter.h"
#include "PlayerArea.h"
#include <iostream>
#include "CharacterFactory.h"
#include "qmessagebox.h"
#include "qfiledialog.h"
#include "AdventureCard.h"
#include "AdventureCardArea.h"
#include <Windows.h>

void Talisman::useAdvCard(void)
{
	AdventureCard * wActiveAdvCard = mAdventureCardArea->getActiveAdventureCard();

	if (0 == wActiveAdvCard)
	{
		QMessageBox msgBox;
		QString msg = "";
		msg.append(QString("No adventure card is currently facing up."));
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
		return;
	}

	if (0 == wActiveAdvCard->getInstruction())
	{
		QMessageBox msgBox;
		QString msg = "";
		msg.append(QString("This adventure card does not have any executable instructions.\nJust read the text and enjoy :)"));
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
		return;
	}

	QMessageBox msgBox;
	QString msg = "";
	msg.append(QString("The instruction for the active adventure card will now be executed!"));
	msgBox.setText(msg);
	msgBox.setIcon(QMessageBox::Information);
	msgBox.exec();

	wActiveAdvCard->getInstruction()->execute(*this);

	mAdventureCardArea->nextCard();
}

/*Editing character properties.*/
void Talisman::editCharacter(void)
{
	if (characterFileName.length() == 0)
	{
		QMessageBox msgBox;
		QString msg = "";
		msg.append(QString("Load a character first!"));
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
		return;
	}
	string launchWhat = "start notepad ";
	launchWhat.append(characterFileName);
	system(launchWhat.c_str());
}

/*Editing map spaces */
void Talisman::editMap(void)
{	
	if (mapFileName.length() == 0)
	{
		QMessageBox msgBox;
		QString msg = "";
		msg.append(QString("Load a map first!"));
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
		return;
	}
	string launchWhat = "start notepad ";
	launchWhat.append(mapFileName);
	system(launchWhat.c_str());

}

/*Loading a character from a set of available characters*/
void Talisman::loadCharacter()
{

	QString qfileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Character documents (*.cha)"));

	if(qfileName.size() == 0)
		return;

	mPlayer=  CharacterFactory::makeCharacter(qfileName.toStdString());

	mArea->setPlayer(mPlayer);
	mArea->update();

	mPositionKeeper->placeCharacter(mPlayer, map->getOuterRegionStartSpace());
	map->setPlayer(mPlayer);
	map->setup();
	characterFileName= qfileName.toStdString();
}

/* Showing the information of the space on which the character lands.*/
void Talisman::showInfo(void)
{
	QMessageBox msgBox;
	QString msg = "";
	msg.append(QString("<b>Space instructions</b>: "));
	msg.append(QString(mPositionKeeper->getCharacterLocation(mPlayer)->getDescription().c_str()));
	msgBox.setText(msg);
	msgBox.setIcon(QMessageBox::Information);
	msgBox.exec();

	// Special instructions - stub!
	if (mPositionKeeper->getCharacterLocation(mPlayer)->getInstruction() != 0)
	{
		mPositionKeeper->getCharacterLocation(mPlayer)->getInstruction()->execute(*this);
	}
}

/*Rolling the die to get a random number between 1 and 6.*/
void Talisman::rollDie()
{
	for (int i = 0; i < 20; ++i)
	{
		Sleep(55);
		die.roll();
		die.update();
		qApp->processEvents();
	}
	QString num;
	num.setNum(die.read());
	mForwardButton->setText("Go Clockwise by " + num);
	mBackButton->setText("Go Counter-Clockwise by " + num);
	map->updateContents();
}


/* Load the map on the game board */
void Talisman::loadMap()
{
	try
	{
		QString qfileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Map documents (*.map)"));

		if(qfileName.size() == 0)
			return;

		talisman_map::Map *oldMap = map;

		map = new talisman_map::Map(mPlayer, mPositionKeeper);
		
		layout->removeWidget(oldMap);
		layout->removeWidget(leftPanel);
		layout->addWidget(map);
		layout->addWidget(leftPanel);


		map->init(qfileName.toStdString());
		mPositionKeeper->placeCharacter(mPlayer, map->getOuterRegionStartSpace());

		map->setup();
		map->updateContents();

		delete oldMap;
		oldMap=0;

		mapFileName =qfileName.toStdString();
	}
	catch (char * e)
	{
		QMessageBox msgBox;
		QString msg (e);
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
	}
}
